﻿/*
 *召唤你 问题经验通用
 */
//控制nav滚动
$(".switcher").scrollLeft(472);
var callyouurl="https://www.pulu.com/webapi/api/PuluQuestion/GetAtmeData_e?userId=";//默认为经验
if(/questionlistzhnw/.test(location.href)){
    callyouurl="https://www.pulu.com/webapi/api/PuluQuestion/GetAtmeData_q?userId=";
}
//跳转方法
function hrefgo(e){
    var url= e.dataset.href;
    var pageid=e.dataset.page;
    var euserid=e.dataset.euser;
    var eid=url.match(/[0-9]+/)[0];
    $.cookie("pulupagescroll",$(window).scrollTop());//当前滚动距离
    $.cookie("pulupagepage",pageid);//当前显示页面
    $.cookie("zhncurrentIndex",zhncurrentIndex);//召唤你数据页码
    location.href=url;
}
$("#heightexpno").click(function(){
    $("#heightexpdiv").toggle(200);
});
//铺路推荐
var zhncurrentIndex=1;
var zhnover=0;//召唤你接口数据状态
var pageSize=10;//一页显示几条新闻
//var newsnum=0;//第几条新闻
var readyover=0;//控制页面滚动0正常 1加载中

//时间 几天前 几分钟前
function getDateTimeStamp(dateStr){
    dateStr=dateStr.match(/[0-9\-T\:]+/)[0].replace(/T/g,' ');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){ result="刚刚";return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
        result="刚刚";
    return result;
}

//召唤你页面  //按钮
var tuijian=document.getElementById("tuijian"); //推荐
var expbtn=document.getElementById("expbtn");  //经验
var askyou=document.getElementById("askyou");   //召唤你
var quebtn=document.getElementById("quebtn"); //问题

var tuijianlist=document.getElementById("tuijianlist");//推荐页面
var explist=document.getElementById("explist");//经验
var askyoulist=document.getElementById("askyoulist");//召唤你
var quelist=document.getElementById("quelist"); //问题
var loca=location.href;
if(askyou){
    askyou.onclick=function(){
        $.cookie("pulupagescroll","");//当前滚动距离
        $.cookie("pulupagepage","");//当前显示页面
        $.cookie("tjcurrentIndex","");//推荐数据页码
        $.cookie("zhncurrentIndex","");//召唤你数据页码
        $.cookie("jycurrentIndex","");//经验 铺路数据页码
        $.cookie("quecurrentIndex","");//问题 铺路数据页码
        location.href="questionlistzhn.html";
    };
}
if(expbtn){
    expbtn.onclick=function(){
        $.cookie("pulupagescroll","");//当前滚动距离
        $.cookie("pulupagepage","");//当前显示页面
        $.cookie("tjcurrentIndex","");//推荐数据页码
        $.cookie("zhncurrentIndex","");//召唤你数据页码
        $.cookie("jycurrentIndex","");//经验 铺路数据页码
        $.cookie("quecurrentIndex","");//问题 铺路数据页码
        location.href="questionlistjy.html";
    };
}
//tuijian.onclick=function(){
//    $.cookie("pulupagescroll","");//当前滚动距离
//    $.cookie("pulupagepage","");//当前显示页面
//    $.cookie("tjcurrentIndex","");//推荐数据页码
//    $.cookie("zhncurrentIndex","");//召唤你数据页码
//    $.cookie("jycurrentIndex","");//经验 铺路数据页码
//    $.cookie("quecurrentIndex","");//问题 铺路数据页码
//    location.href="questionlisttj.html";
//};
if(quebtn){
    quebtn.onclick=function(){
        $.cookie("pulupagescroll","");//当前滚动距离
        $.cookie("pulupagepage","");//当前显示页面
        $.cookie("tjcurrentIndex","");//推荐数据页码
        $.cookie("zhncurrentIndex","");//召唤你数据页码
        $.cookie("jycurrentIndex","");//经验 铺路数据页码
        $.cookie("quecurrentIndex","");//问题 铺路数据页码
        location.href="questionlisttw.html";
    };
}

//付费分享
var sharepay=document.getElementsByClassName("sharepay");
var sharepaymob=document.getElementsByClassName("sharepaymob")[0];
var cancel3=document.getElementsByClassName("cancel3")[0];
var sharepayword=document.getElementsByClassName("sharepayword")[0];
var askpayword=document.getElementsByClassName("askpayword")[0];
var modalbg=document.getElementsByClassName("modal_bg2")[0];
for(var sh=0;sh<sharepay.length;sh++){
    sharepay[sh].onclick=function(){
        modalbg.style.display="block";
        sharepaymob.style.display="block";
        sharepayword.style.display="block";
        askpayword.style.display="none";
    }
}
cancel3.onclick=function(){
    if(modalbg.style.display=="block"&&sharepaymob.style.display=="block"){
        modalbg.className+=' animate_out';
        sharepaymob.className+=' animate_out';
    }
    timer=setTimeout(changeclass3,100);
};
//付费充值跳转
okgobtn.onclick=function(){
    modalbg.className+=' animate_out';
    sharepaymob.className+=' animate_out';
    location.href="scoredetial.html";
};
function changeclass3(){
    modalbg.className='modal_bg modal_bg';
    sharepaymob.className='action_menu_container sharepaymob';
    clearTimeout();
    timer2=null;
    changedisplay3();
};
function changedisplay3(){
    modalbg.style.display="none";
    sharepaymob.style.display="none";
};
//付费回答
var askpay=document.getElementsByClassName("askpay");
for(var as=0;as<askpay.length;as++){
    askpay[as].onclick=function(){
        modalbg.style.display="block";
        sharepaymob.style.display="block";
        sharepayword.style.display="none";
        askpayword.style.display="block";
    }
}
cancel3.onclick=function(){
    if(modalbg.style.display=="block"&&sharepaymob.style.display=="block"){
        modalbg.className+=' animate_out';
        sharepaymob.className+=' animate_out';
    }
    timer=setTimeout(changeclass3,100);
};
var qtype;
if(/questionlistzhnw/.test(location.href)){
    qtype=2;
}else{
    qtype=3;
}
function gohrefftnerr(){
    var e=arguments.callee.caller.arguments[0]||event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容
    if ( e && e.stopPropagation ) {
        // 因此它支持W3C的stopPropagation()方法
        e.stopPropagation();
    } else {
        // 否则，我们需要使用IE的方式来取消事件冒泡
        window.event.cancelBubble = true;
    }
}
//所有方法
function allftn(){
    console.log("执行方法");
    $().ready(function(){
        if($.cookie("pulupagepage")){
            $("#askyoulist div.addlistplace div").remove();
            var pagelist=document.getElementsByClassName("HomeMainFeed");
            var navlist=document.getElementsByClassName("HeaderSwitcherItem");
            readyover=1;//控制页面滚动
            //召唤你
            zhnpageSize=parseFloat($.cookie("zhncurrentIndex"))*10;
            zhn(1,zhnpageSize);
            zhncurrentIndex=parseFloat($.cookie("zhncurrentIndex"));
        }
        else{

            if(id){
                zhn(zhncurrentIndex,pageSize);
            }
        }
    });
    var upanddown=127;
    $(window).scroll(function(){
        $(".quicklist").css("display","none");
        $("#refreshbtn").css("display","block");
        var mainheight=$(".home_feed").height();
        var windowheight=$(window).height();
        var what=$(".home_feed").offset().top;
        var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
        var cz=windowheight-mainheight+50;
        if(chajuli<cz&&readyover===0){
            console.log("召唤你页面滚动");
            zhncurrentIndex++;
            readyover=1;
            zhn(zhncurrentIndex,pageSize);
        }
        //导航条隐藏显示
        //var a2=chajuli;
        //if(a2<upanddown){
        //    upanddown=a2;
        //    //向下滚动
        //    if(chajuli<36){
        //        $(".fixed_header").css({"position":"fixed","top":"-44px"});
        //        $(".wrapper.home_feed").css({"margin-top":"44px"});
        //        $("#page_wrapper").css("margin-top","44px");
        //    }
        //}
        //else{
        //    upanddown=a2;
        //    //向上滚动
        //    if(0<chajuli){
        //        $(".fixed_header").css({"position":"absolute","top":"0px"});
        //        $(".wrapper.home_feed").css({"margin-top":"0"});
        //        $("#page_wrapper").css("margin-top","83px");
        //    }
        //}

    });
    //刷新页面
    //$("#refreshbtn").click(function(){
    //    zhncurrentIndex=1;
    //    $("#waiting3").css("display","block");
    //    $("#nonews3").css("display","none");
    //    $(".fixed_header").css({"position":"absolute","top":"0px"});
    //    $(".wrapper.home_feed").css({"margin-top":"0"});
    //    $("#page_wrapper").css("margin-top","127px");
    //    $("#askyoulist .addlistplace div").remove();
    //    zhn(zhncurrentIndex,pageSize);
    //});
    //召唤你数据
    function zhn(zhncurrentIndex,pageSize){
        if(zhnover===0){
            $.ajax({
                type:"get",
                datatype:"json",
                url:callyouurl+id+"&currentIndex="+zhncurrentIndex+"&pageSize="+pageSize+"&qtype="+qtype,
                success:function(d){
                    console.log("召唤你");
                    console.log(d);
                    if(d.Message=="没有数据"||!d.Data|| d.Data.length==0){
                        zhnover=1;
                        console.log("zhnover没有数据了readyover："+readyover);
                        $("#waiting3").css("display","none");
                        $("#nonews3").css("display","block");
                    }else if(d.Data&&d.Data.length>0&&d.Data.length<pageSize){
                        $("#waiting3").css("display","none");
                        $("#nonews3").css("display","block");
                    }
                    var ulid="#askyoulist";
                    datafun(d,ulid,zhnover);//页面样式一样 通用方法
                }
            })
        }
    }
//通用方法，样式差不多的 推荐
    function datafun(d,ulid,num){
        var fortime=0;//记录循环次数
        if(d.Message=="没有数据"||!d.Data||d.Data.length==0){
            num=1;
            readyover=0;
            console.log("没有数据了readyover："+readyover);

        }else{
            for(var i= 0,r=d.Data.length;i<r;i++){
                fortime++;
                d.Data[i].index=i;
                if(d.Data[i].title&&d.Data[i].title.length>0){
                    var unick="";
                    if(d.Data[i].isHideName==1){
                        unick="<a onclick='gohrefftnerr(this)'>匿名</a>";
                    }else{
                        unick="<a href='otherpeoplepage.html?otherid="+d.Data[i].questionUserId+"'>"+d.Data[i].questionUserNick+"(已赚"+(d.Data[i].userEarnScore)/100+"元)</a>";
                    }
                    //头像
                    var uimgword="";
                    if(d.Data[i].questionUserAva&&d.Data[i].isHideName!=1){
                        if(d.Data[i].isHideName!=1){
                            uimgword="<a href='otherpeoplepage.html?otherid="+d.Data[i].questionUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.Data[i].questionUserAva+"'></a>";
                        }else{
                            uimgword="<img onclick='gohrefftnerr(this)' class='profile_photo_img' src='../img/af8.png'>";
                        }
                    }else{
                        if(d.Data[i].isHideName!=1){
                            uimgword="<a href='otherpeoplepage.html?otherid="+d.Data[i].questionUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png'></a>";
                        }else{
                            uimgword="<img onclick='gohrefftnerr(this)' class='profile_photo_img' src='../img/af8.png'>";
                        }
                    };
                    //有没有图片
                    var haveimg="";
                    //if(d.Data[i].imgcount>0){
                    //    haveimg="<span class='isheighlogo haveimg'></span>"
                    //}
                    if(d.Data[i].imgcount>0){
                        haveimg="(图)"
                    }
                    var money="";//有没有钱
                    if(d.Data[i].score>0){
                        var score= parseFloat(d.Data[i].score)/100;
                        money="<span class='havemy'>¥ "+score+"</span>";
                    }
                    var tim="";
                    if(d.Data[i].ctime){
                        tim=getDateDiff(getDateTimeStamp(d.Data[i].ctime));
                    }
                    var count=0;
                    if(d.Data[i].browerCount){
                        count=d.Data[i].browerCount;
                    }
                    var des="";
                    if(d.Data[i].des){
                        des=codetoface(d.Data[i].des);
                    }
                    var sharetim="";
                    d.Data[i].atMeTime=d.Data[i].atMeTime.match(/[0-9\-T\:]+/)[0];
                    sharetim=getDateDiff(getDateTimeStamp(d.Data[i].atMeTime));
                    //关键词
                    var keywordnor;
                    var keyword="<span class='keywordtit'>关键词：</span>";
                    if(d.Data[i].keywords){
                        keywordnor=d.Data[i].keywords.split(",");
                        for(var a=0;a<keywordnor.length;a++){
                            keyword+="<span>"+keywordnor[a]+"</span>";
                        }
                    }
                    //是不是转换
                    var isconvert="";
                    if(d.Data[i].isconvert&&d.Data[i].isconvert===1){
                        isconvert="(问题转经验)"
                    }
                    //召唤者是不是匿名
                    var atmeUserNick=d.Data[i].atMeUserNick;
                    if(d.Data[i].atmeIsHideName==1){
                        atmeUserNick="匿名";
                    }
                    //问题
                    if(d.Data[i].type==2){
                        var location="";
                        if(d.Data[i].puluLocation){
                            location="<div style='position:relative;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:3px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.Data[i].puluLocation+"</span></div>"
                        }
                        var tit=codetoface(d.Data[i].title);
                        $("#askyoulist .addlistplace").append("<div class='pagedlist_item shareexperience' onclick='hrefgo(this)' data-money='0' data-page='"+ulid+"' data-isheight='0' data-href='questiondetail.html?qid="+d.Data[i].id+"#fromuser="+id+"' data-euser='"+d.Data[i].questionUserId+"'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'><a href='otherpeoplepage.html?otherid="+d.Data[i].atMeUserId+"'>"+ atmeUserNick+" 提到了你"+"</a></span> "+sharetim+"</div><div class='info_wrapper' style='margin-left:10px;margin-top:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> </div> <div class='borderbox'><div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+"</span> <div class='ContentFooter'>"+tim+"</div></div></div></div><div class='question_topics_container'>"+"</div><h2><a class='question_link'>"+isconvert+tit+haveimg+"</a></h2><div class='ActionBar Question'>"+location+"<div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>浏览 "+count+"&nbsp;&nbsp;&nbsp;回答 "+d.Data[i].answerCount+"</a> </div> <div class='action_item'> <a class='ReadMoreActionItem'></a> </div></div></div><div class='QuestionTopics has_topics'>"+keyword+"</div></div></div></div>")
                    }
                    //经验
                    else{
                        var location="";
                        if(d.Data[i].puluLocation){
                            location="<div style='position:relative;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:2px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.Data[i].puluLocation+"</span></div>"
                        }
                        //高额经验
                        var isheilogo="";
                        if(d.Data[i].isExphigh===1){
                            isheilogo="<div class='isheighlogo'></div>"
                        }
                        var userimg="";//保存头像代码段 修改评星
                        //评星
                        var starlen=0;
                        if(d.Data[i].starScore!==0){
                            var strnum=(d.Data[i].starScore).toString();
                            if(strnum.length===3){
                                var lastnum=parseFloat(strnum.substring(strnum.length-1));
                                //尾数在4~6之间
                                if(lastnum>=4&&lastnum<=6){
                                    starlen=strnum.substring(strnum.length-1,0)+5;
                                }else if(lastnum<4){
                                    starlen=strnum.substring(0,1);
                                }else if(lastnum>6){
                                    starlen=parseFloat(strnum.substring(0,1))+1;
                                }
                            }else{
                                starlen=d.Data[i].starScore;
                            }

                        }
                        userimg="<div class='starimg'><i style='width:"+parseFloat(starlen)*20+"%;'></i></div>";
                        var tit=codetoface(d.Data[i].title);
                        $("#askyoulist .addlistplace").append("<div class='pagedlist_item shareexperience' onclick='hrefgo(this)' data-money='"+d.Data[i].score+"' data-isheight='"+d.Data[i].isExphigh+"' data-page='"+ulid+"' data-href='shareexppage.html?eid="+d.Data[i].id+"#fromuser="+id+"' data-euser='"+d.Data[i].questionUserId+"'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'><a href='otherpeoplepage.html?otherid="+d.Data[i].atMeUserUserId+"'>"+ atmeUserNick+" 提到了你"+"</a></span> "+sharetim+"</div><div class='info_wrapper' style='margin-left:10px;margin-top:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> </div> <div class='borderbox'><div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+isheilogo+"</span> <div class='ContentFooter'>"+tim+"</div></div></div></div><div class='question_topics_container'>"+"</div><h2><a class='question_link'>"+isconvert+tit+haveimg+"("+d.Data[i].wordcount+"字)</a></h2><div class='upviter'>"+userimg+"</div><div class='ActionBar Question'>"+location+"<div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>浏览 "+count+"&nbsp;&nbsp;&nbsp;付费 "+d.Data[i].payCount+"</a> </div> <div class='action_item'> <a class='ReadMoreActionItem'></a> </div></div></div><div class='QuestionTopics has_topics'>"+keyword+"</div></div></div></div>")
                    }
                };
                //页面滚动判断
                if(ulid===$.cookie("pulupagepage")&&fortime===r){
                    $(window).scrollTop(parseFloat($.cookie("pulupagescroll")));
                    $.cookie("pulupagepage","");//清除页面ID
                    $.cookie("pulupagescroll","");//当前滚动距离
                    $.cookie("pulupagepage","");//当前显示页面
                    $.cookie("tjcurrentIndex","");//推荐数据页码
                    $.cookie("zhncurrentIndex","");//召唤你数据页码
                    $.cookie("jycurrentIndex","");//经验 铺路数据页码
                    $.cookie("quecurrentIndex","");//问题 铺路数据页码
                }
                if(fortime===r){
                    readyover=0;
                }
            };
            num=0;
        }
    };
}
var id;
if($.cookie("MYID")){id=$.cookie("MYID")};
var returnurl=encodeURIComponent("https://www.pulu.com/weixin/questionlistzhn.html"); //同意后跳转页面
$(document).ready(function(){
    if(is_weixn()){
        if($.cookie("wxopenid")){
            //====================================================================
            //从登录页返回有openid没有id
            if($.cookie("MYID")){
                allftn();
            }
            else{
                location.href="login.html";
            }
            //====================================================================
        }
        else{
            //先微信验证获取openid 如果已经授权过了 直接页面截取openid 然后发送请求获取 用户的ID
            var reg=new RegExp("code=[a-zA-Z0-9]*");
            if(window.location.href.match(reg)){
                var code=window.location.href.match(reg);
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){
                        var dat=data;
                        $.cookie("wxod",dat);
                        //发请求获取用户的id
                        $.ajax({
                            type:"GET",
                            url:getwxopenidurl+dat,
                            success:function(d){
                                //如果没有用户id 则证明用户是第一次登陆 保存openid跳转至登录页面
                                if(!d.Data){
                                    $.cookie("wxopenid",dat);
                                    location.href="login.html";
                                }else if(d.Data&&d.Data.userId){
                                    $.cookie("wxopenid",d.Data.openId);
                                    $.cookie("MYID",d.Data.userId);
                                    id=d.Data.userId;
                                    //================================================================
                                    allftn();
                                    //================================================================
                                }
                                //如果有用户ID 就把cookies里的用户id替换成获取到的id  在这里控制跳转提示框
                            }
                        })
                    }
                })
            }else{
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            }
        }
    }else{
        allftn();
    }
})